/***************************************************************************
 *   Copyright (C) 2007 by Sebastian Bilski, Lance Cooper, Jeff Rebacz,
 *   									and Alex Zelenskiy
 *   sbilski@iit.edu
 *   lance.m.cooper@gmail.com
 *   jeff.rebacz@gmail.com
 *   voiceofanarchy@gmail.com
 *
 *	This file is part of connect3
 *
 *   connect3 is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License, version 2,
 *    as published by the Free Software Foundation
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc., 
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 ***************************************************************************/

public class GameBoard {
	public int xDimension;
	public int yDimension;
	public GamePiece[][] board;


	public GameBoard() {
		super();
		xDimension=1;
		yDimension=1;
		board=new GamePiece[xDimension][yDimension];
	}

	public GameBoard(int x, int y) {
		super();
		setXDimension(x);
		setYDimension(y);
		board=new GamePiece[xDimension][yDimension];
	}

	public synchronized int makeMove(GamePiece gp, int x){		

		if(x<0||x>=xDimension){
			System.err.println("invalid make move in GameBoard class");
			return -1;			
		}
		
		int yPosition=-1;
		for(int y = 0; y < yDimension; y++){			
			if(board[x][y]==null)
			{
				yPosition = y;
				break;
			}
		}

		if(yPosition >= 0){
			board[x][yPosition] = gp;
			gp.setY(yPosition);
			return yPosition;
		}else{
			return -1;
		}

	}

	public void undoMove(){

	}


	public synchronized void reset(){
		board = new GamePiece[xDimension][yDimension];
	}

	public void destroy(){
		board = new GamePiece[xDimension][yDimension];
	}

	public void saveBoard(){

	}


	public GamePiece getPieceAt(int x, int y){
		if(x<0||y<0||x>=xDimension||y>=yDimension){
			return null;
		}
		return board[x][y];		
	}

	public synchronized void setPieceAt(int x, int y, GamePiece gp){
		if(x>=0 && y>=0 && x<xDimension && y<yDimension){
			board[x][y] = gp;		
		}else{
			System.err.println("attempt to set piece out of bounds");
		}
	}

	public int getXDimension() {
		return xDimension;
	}
	public synchronized void setXDimension(int x) {
		if(x>0){
			xDimension = x;
		}else{
			System.err.println("attempt to set XDimension to "+x);
			xDimension=1;
		}
	}
	public int getYDimension() {

		return yDimension;
	}
	public synchronized void setYDimension(int y) {
		if(y>0){
			yDimension = y;
		}else {
			System.err.println("attempt to set XDimension to "+y);
			yDimension = 1;
		}
	}

}
